'use strict';
const os = require('os');

module.exports = {
  parseMsg(action, payload = {}, metadata = {}) {
    const meta = Object.assign(
      {},
      {
        time: Date.now(),
      },
      metadata,
    );

    return {
      meta,
      data: {
        action,
        payload,
      },
    };
  },

  // 获取本地ip
  getIPAdress() {
    const interfaces = os.networkInterfaces();
    const ips = [];
    for (var devName in interfaces) {
      var iface = interfaces[devName];
      for (var i = 0; i < iface.length; i++) {
        var alias = iface[i];
        if (alias.family === 'IPv4' && alias.address !== '127.0.0.1' && !alias.internal) {
          ips.push(alias.address);
        }
      }
    }
    return ips;
  },
};
